<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1>登录</h1>
        <p>欢迎回来</p>
      </div>

      <a-form
        :model="formData"
        :rules="rules"
        layout="vertical"
        class="login-form"
      >
        <a-form-item field="account" label="用户名">
          <a-input
            v-model="formData.account"
            placeholder="请输入用户名"
            size="large"
          >
            <template #prefix>
              <icon-user />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item field="password" label="密码">
          <a-input-password
            v-model="formData.password"
            placeholder="请输入密码"
            size="large"
          >
            <template #prefix>
              <icon-lock />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            size="large"
            :loading="loading"
            long
            @click="handleSubmit"
          >
            登录
          </a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import { IconUser, IconLock } from '@arco-design/web-vue/es/icon'
import { login } from '@/api/auth'
import to from '@/utils/to'
import { setToken } from '@/utils/auth'
import { useRouter } from 'vue-router'
const router = useRouter()
// 表单数据
const formData = reactive({
  account: '',
  password: '',
})

// 加载状态
const loading = ref(false)

// 表单验证规则
const rules = {
  account: [{ required: true, message: '请输入用户名' }],
  password: [
    { required: true, message: '请输入密码' },
    { minLength: 6, message: '密码长度不能少于6位' },
  ],
}

// 提交表单
const handleSubmit = async () => {
  loading.value = true
  console.log(123123)
  const data = {
    account: formData.account,
    password: formData.password,
  }
  // 模拟登录请求
  const [res, err] = await to<LoginRes>(login(data))
  console.log(err)
  if (res) {
    const token = res.data.token
    setToken(token)
    Message.success('登录成功')
    router.push({
      path: '/',
    })
  }
  loading.value = false
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
}

.login-header h1 {
  margin: 0 0 8px 0;
  color: #1d2129;
  font-size: 28px;
  font-weight: 600;
}

.login-header p {
  margin: 0;
  color: #86909c;
  font-size: 14px;
}

.login-form {
  margin-top: 24px;
}

:deep(.arco-form-item-label) {
  font-weight: 500;
  color: #1d2129;
}

:deep(.arco-input-wrapper) {
  border-radius: 8px;
}

:deep(.arco-btn-primary) {
  border-radius: 8px;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
}
</style>
